package com.qinge.blog.service.impl;

import com.qinge.blog.dao.BlogContentDao;
import com.qinge.blog.dao.BlogContentTypeDao;
import com.qinge.blog.dao.BlogTypeDao;
import com.qinge.blog.entity.*;
import com.qinge.blog.service.BlogContentTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022/5/16 16:38
 * @description 内容分类 实现类
 **/
@Service
public class BlogContentTypeServiceImpl implements BlogContentTypeService {
    @Resource
    BlogContentTypeDao dao;

    @Resource
    BlogContentDao contentDao;

    @Resource
    BlogTypeDao typeDao;

    @Override
    public void add(BlogContentType... contentTypes) {
        dao.batchInsert(Arrays.asList(contentTypes));
    }

    @Override
    public void addByList(List<BlogContentType> contentTypes) {
        dao.batchInsert(contentTypes);
    }

    @Override
    public void removeByConIds(String... conIds) {

    }

    @Override
    public void removeByTypeIds(String... typeIds) {
        removeByConIdList(Arrays.asList(typeIds));
    }

    @Override
    public void removeByConIdList(List<String> conIds) {
        BlogContentTypeExample example=new BlogContentTypeExample();
        BlogContentTypeExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdIn(conIds);

        dao.deleteByExample(example);
    }

    @Override
    public void removeByTypeIdsList(List<String> typeIds) {
        BlogContentTypeExample example=new BlogContentTypeExample();
        BlogContentTypeExample.Criteria criteria=example.createCriteria();

        criteria.andTypeIdIn(typeIds);

        dao.deleteByExample(example);
    }

    @Override
    public List<BlogContent> findContentsByTypeId(String typeId) {
        BlogContentTypeExample example=new BlogContentTypeExample();
        BlogContentTypeExample.Criteria criteria=example.createCriteria();

        criteria.andTypeIdEqualTo(typeId);

        List<BlogContentType> blogContentTypes = dao.selectByExample(example);

        BlogContentExample contentExample=new BlogContentExample();
        BlogContentExample.Criteria criteria1=contentExample.createCriteria();

        return contentDao.selectByExample(contentExample);
    }

    @Override
    public int findCountByTypeId(String typeId) {
        BlogContentTypeExample example=new BlogContentTypeExample();
        BlogContentTypeExample.Criteria criteria=example.createCriteria();

        criteria.andTypeIdEqualTo(typeId);

        List<BlogContentType> blogContentTypes = dao.selectByExample(example);

        return blogContentTypes.size();

    }

    @Override
    public BlogType findTypeByConId(String ConId) {
        BlogContentTypeExample example=new BlogContentTypeExample();
        BlogContentTypeExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdEqualTo(ConId);

        List<BlogContentType> blogContentTypes = dao.selectByExample(example);

        if(blogContentTypes.size()==0){
            return null;
        }

        return  typeDao.selectByPrimaryKey(blogContentTypes.get(0).getTypeId());
    }
}
